const $ = jQuery = require('jquery')
require('popper.js')
require('bootstrap')

const BASE_URL = 'https://gank.io/api/v2'
// const BASE_URL = 'http://localhost:8877/api/v2'

$.ajax({
  type: 'GET',
  url: `${BASE_URL}/banners`,
  success(res) {
    res.data.map(function (item, index) {
      // console.log(item)
      // console.log($('.carousel-inner'))
      $(`<div class="carousel-item ${index === 0 && 'active'}" style="background-image: url(${item.image})"></div>`).appendTo('.carousel-inner')
      $(`<li data-target="#carouselExampleIndicators" data-slide-to="${index}" class="${index === 0 && 'active'}"></li>`).appendTo('.carousel-indicators')
      /*$(`<div class="carousel-item ${index === 0 && 'active'}">
          <img src="${item.image}" class="d-block w-100" alt="${item.title}">
        </div>`).appendTo('.carousel-inner')*/
    })
  }
})

$.ajax({
  url: `${BASE_URL}/data/category/Girl/type/Girl/page/2/count/50`,
  success(res) {
    console.log(res.data)
    res.data.map((item, index) => {
      // console.log(new Date(item.publishedAt).getTime() / 1000)
      let pAt = Math.ceil(new Date(item.publishedAt).getTime() / 1000)
      // 创建时间
      // let pAt = Math.ceil(new Date('2021-03-25').getTime() / 1000)
      let range = Math.ceil(new Date() / 1000) - pAt
      let timer = range / (30 * 24 * 60 * 60)

      // 30 * 24 * 60 * 60

      // 距离现在多少秒
      // let range = Math.ceil(new Date() / 1000) - pAt
      // console.log(Math.ceil(new Date() / 1000) - pAt)
      // if (range < )
      // 今日凌晨开始
      // let today = new Date()
      // let t_time = Math.ceil(new Date(`${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()} 00:00:00`) / 1000)

      $(`<div class="col-4">
          <div class="card mb-3">
            <img src="${item.url}" class="card-img-top">
            <div class="card-body">
              <h5 class="card-title">${item.title}</h5>
              <p class="card-text">${item.desc}</p>
              <p class="card-text">
                <small class="text-muted">${timer.toFixed()}月之前</small>
              </p>
            </div>
          </div>
        </div>`).appendTo('.my-list')
    })
  }
})

$('.nav').on('click', '.nav-item a', function () {
  console.info(this)
  console.log($(this).data('category'))
  console.log($(this).data('type'))

  $.ajax({
    url: `${BASE_URL}/data/category/${$(this).data('category')}/type/${$(this).data('type')}/page/1/count/50`,
    success(res) {
      console.log(res.data)
      $('.my-list').html('')
      res.data.map((item, index) => {
        // console.log(new Date(item.publishedAt).getTime() / 1000)
        let pAt = Math.ceil(new Date(item.publishedAt).getTime() / 1000)
        // 创建时间
        // let pAt = Math.ceil(new Date('2021-03-25').getTime() / 1000)
        let range = Math.ceil(new Date() / 1000) - pAt
        let timer = range / (30 * 24 * 60 * 60)

        $(`<div class="col-4">
          <div class="card mb-3">
            <img src="${item.images[0]}" class="card-img-top">
            <div class="card-body">
              <h5 class="card-title">${item.title}</h5>
              <p class="card-text">${item.desc}</p>
              <p class="card-text">
                <small class="text-muted">${timer.toFixed()}月之前</small>
              </p>
            </div>
          </div>
        </div>`).appendTo('.my-list')
      })
    }
  })
})

